package com.peanut.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.domain.bo.TabCarSeriesBO;
import com.peanut.domain.po.TabBrandSeriesPO;
import com.peanut.domain.po.TabCarSeriesPO;
import com.peanut.mapper.TabBrandSeriesMapper;
import com.peanut.service.TabCarSeriesService;
import com.peanut.mapper.TabCarSeriesMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 张涛
* @description 针对表【tab_car_series】的数据库操作Service实现
* @createDate 2022-05-26 17:43:44
*/
@Service
public class TabCarSeriesServiceImpl extends ServiceImpl<TabCarSeriesMapper, TabCarSeriesPO>
    implements TabCarSeriesService{

    @Resource
    private TabCarSeriesMapper seriesMapper;

    @Resource
    private TabBrandSeriesMapper brandSeriesMapper;

    @Override
    public int addSeries(TabCarSeriesBO bo, Long carBrandId) {
        TabCarSeriesPO series = new TabCarSeriesPO();
        BeanUtils.copyProperties(bo,series);
        int i = seriesMapper.insertReturnId(series);
        TabBrandSeriesPO brandSeries = new TabBrandSeriesPO();
        brandSeries.setCarBrandId(carBrandId);
        brandSeries.setCarSeriesId(series.getCarSeriesId());
        brandSeriesMapper.insert(brandSeries);
        return i;
    }

    @Override
    public Page<TabCarSeriesPO> findAll(Integer pageNumber, Integer pageSize) {
        Page<TabCarSeriesPO> page = new Page<>(pageNumber,pageSize);
        Page<TabCarSeriesPO> page1 = seriesMapper.selectPage(page, null);
        return page1;
    }

    @Override
    public int updateSeries(TabCarSeriesBO bo, Long carBrandId) {
        TabCarSeriesPO series = new TabCarSeriesPO();
        BeanUtils.copyProperties(bo,series);
        int i = seriesMapper.updateById(series);
//        TabBrandSeriesPO brandSeries = new TabBrandSeriesPO();
//        brandSeries.setCarBrandId(carBrandId);
//        brandSeries.setCarSeriesId(series.getCarSeriesId());
//        int car_series_id = brandSeriesMapper.update(brandSeries, new QueryWrapper<TabBrandSeriesPO>().eq("car_series_id", series.getCarSeriesId()));
        return i;
    }

    @Override
    public int deleteSeries(Long carSeriesId) {
        int i = seriesMapper.deleteById(carSeriesId);
        int car_series_id = brandSeriesMapper.delete(new QueryWrapper<TabBrandSeriesPO>().eq("car_series_id", carSeriesId));
        return i;
    }
}




